﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->listWidget->setResizeMode(QListView::Adjust);
    ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    initUI();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    ui->listWidget->clear();
    QStringList list={"语文","美术","体育","音乐","地球物理学","数学","英语"};
    QIcon icon;
    icon.addFile(":/image/class.png");
    QFont font=ui->listWidget->font();
    font.setPointSize(30);
    ui->listWidget->setFont(font);
    ui->listWidget->setIconSize(QSize(40,40));
    for(int i=0;i<list.size();i++)
    {
        QListWidgetItem*item=new QListWidgetItem;
        item->setCheckState(Qt::Unchecked);//设置未勾选的复选框
        item->setText(list[i]);
        item->setIcon(icon);
        item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsUserCheckable);
        //item->setSizeHint(QSize(200,80));
        ui->listWidget->addItem(item);
    }

    QMenu*menu=new QMenu(this);
    menu->addAction(ui->action_icon);
    menu->addAction(ui->action_list);
    menu->addAction(ui->action_delete);
    ui->toolButton->setMenu(menu);
    ui->toolButton->setPopupMode(QToolButton::InstantPopup);
    ui->toolButton->setDefaultAction(ui->action_icon);
    ui->toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

}

void MainWindow::on_action_icon_triggered()
{
    ui->listWidget->setViewMode(QListView::IconMode);
    //ui->listWidget->setFlow(QListView::LeftToRight);
    ui->listWidget->setSpacing(10);
}

void MainWindow::on_action_list_triggered()
{
    ui->listWidget->setViewMode(QListView::ListMode);
    ui->listWidget->setFlow(QListView::TopToBottom);
}

void MainWindow::on_action_delete_triggered()
{
    int count=ui->listWidget->count();
    for(int i=count-1;i>=0;i--)
    {
        if(ui->listWidget->item(i)->checkState()==Qt::Checked)
        {
            auto item=ui->listWidget->takeItem(i);
            delete item;
        }
    }
}

void MainWindow::on_action_selectAll_triggered()
{
    int count=ui->listWidget->count();
    for(int i=0;i<count;i++)
    {
        ui->listWidget->item(i)->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_action_invertSelect_triggered()
{
    int count=ui->listWidget->count();
    for(int i=0;i<count;i++)
    {
        if(ui->listWidget->item(i)->checkState()==Qt::Unchecked)
        {
            ui->listWidget->item(i)->setCheckState(Qt::Checked);
        }
        else
        {
            ui->listWidget->item(i)->setCheckState(Qt::Unchecked);
        }

    }
}

void MainWindow::on_pushButton_clicked()
{

    QIcon icon;
    icon.addFile(":/image/class.png");
    if(ui->lineEdit->text().trimmed().length()>0)
    {
        QListWidgetItem *item=new QListWidgetItem;
        item->setText(ui->lineEdit->text().trimmed());
        item->setIcon(icon);
        item->setCheckState(Qt::Unchecked);
        ui->listWidget->addItem(item);
    }
    ui->lineEdit->clear();
}

void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    QMenu*menu=new QMenu(this);
    menu->addAction(ui->action_icon);
    menu->addAction(ui->action_list);
    menu->addSeparator();
    menu->addAction(ui->action_delete);
    menu->exec(QCursor::pos());
    delete menu;
}
